package com.ylt.controller.pay;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.utils.ylt.SwiftpassConfig;
import com.ylt.controller.WeChatController;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.Log4JLoggerFactory;

public class XcxPay {
	
	InternalLogger log = Log4JLoggerFactory.getInstance(XcxPay.class);

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public Map  getSessionKeyOropenid(String code) {
        Map returnMap = new HashMap();
        //微信端登录code值
        String wxCode = code;
        try {
            String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";  //请求地址 https://api.weixin.qq.com/sns/jscode2session
            Map<String, String> requestUrlParam = new HashMap<String, String>();
            requestUrlParam.put("appid", "wx2ec83ffb41e9ba6d");  //开发者设置中的appId
            requestUrlParam.put("secret", "4d3da01850ac1294a994b9af92d7ff98"); //开发者设置中的appSecret
            requestUrlParam.put("js_code", wxCode); //小程序调用wx.login返回的code
            requestUrlParam.put("grant_type", "authorization_code");    //默认参数 authorization_code

            //发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
            JSONObject jsonObject = JSON.parseObject(sendPost(requestUrl, requestUrlParam));
            if (jsonObject.get("openid") !=null) {
                returnMap.put("openId",jsonObject.get("openid"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.debug(e);
        }

        return returnMap;
    }
	
	public  String sendPost(String url, Map<String, ?> paramMap) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";

        String param = "";
        Iterator<String> it = paramMap.keySet().iterator();

        while (it.hasNext()) {
            String key = it.next();
            param += key + "=" + paramMap.get(key) + "&";
        }

        try {
            URL realUrl = new URL(url+"?"+param);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.connect();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            log.debug("line====="+line);
        } catch (Exception e) {
            System.out.println(e);
            log.debug(e);
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
                log.debug(ex);
            }
        }
        return result;
    }

}
